package com.douya.mvc.config;

import com.douya.mvc.property.DouYaMvcProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;

/**
 * swagger配置
 *
 * @Author hcy
 * @Date 2022/11/24
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Resource
    DouYaMvcProperties douYaMvcProperties;

    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * select() 返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现
     */
    @Bean
    public Docket createRestApi() {
        String swaggerScanPath = douYaMvcProperties.getSwaggerScanPath();
        // swaggerScanPath 为扫描的包路径 以逗号分隔 转为分号分隔
        swaggerScanPath = swaggerScanPath.replaceAll(",", ";");
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                // 为当前包路径
                .apis(RequestHandlerSelectors.basePackage(swaggerScanPath))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     *
     * @return ApiInfo
     */
    private ApiInfo apiInfo() {
        String swaggerTitle = douYaMvcProperties.getSwaggerTitle();
        String swaggerDescription = douYaMvcProperties.getSwaggerDescription();
        String swaggerVersion = douYaMvcProperties.getSwaggerVersion();
        return new ApiInfoBuilder()
                // 页面标题
                .title(swaggerTitle)
                // 创建人
                .contact(new Contact("开发", "", ""))
                // 版本号
                .version(swaggerVersion)
                // 描述
                .description(swaggerDescription)
                .build();
    }
}
